package net.projecteuler.util;

import java.util.ArrayList;
import java.util.List;

public class PrimeUtil {
	public static List<Integer> PrimeList = null;
	
	static{
		PrimeList = getPrimeList(100000);
	}
	
	public static List<Integer> getPrimeList(int max){
		List<Integer> primeListTemp = new ArrayList<Integer>();
		
		for(int i = 2; i <= max; i++){
			boolean bPrime = true;
			
			for(int j = 0; j < primeListTemp.size(); j++){
				int smallerPrime = primeListTemp.get(j);
				
				if(i % smallerPrime == 0){
					bPrime = false;
					break;
				}
				
				if((long)i < (long)smallerPrime * (long)smallerPrime){
					break;
				}
			}
			
			if(bPrime){
				primeListTemp.add(i);
			}
		}
		
		return primeListTemp;
	}
	
	public static boolean isPrime(int value){
		for(int i = 0; i < PrimeList.size(); i++){
			int prime = PrimeList.get(i);
			
			if(value % prime == 0){
				return false;
			}
			
			if((long)value < (long)prime * (long)prime)
				break;
		}
		
		return true;
	}
}
